package cn.dyw.cloudmusic.http;

import cn.dyw.cloudmusic.music.vo.MusicComment;
import cn.dyw.cloudmusic.http.model.CommentParams;
import cn.dyw.cloudmusic.http.model.Params;
import cn.dyw.cloudmusic.utils.OkHttpUtils;
import com.alibaba.fastjson.JSON;
import lombok.extern.log4j.Log4j;
import okhttp3.Response;

/**
 * @author dyw770
 */
@Log4j
public class CommentSaveHandleProxy implements SaveHandle {


    private boolean more = true;

    private SaveHandle commentSaveHandle;

    public boolean isMore() {
        return more;
    }

    public SaveHandle getCommentSaveHandle() {
        return this.commentSaveHandle;
    }

    public CommentSaveHandleProxy(SaveHandle commentSaveHandle) {
        this.commentSaveHandle = commentSaveHandle;
    }

    @Override
    public void save(Response response, Params p) {
        // 获取返回结果 代理save方法
        String result = OkHttpUtils.stringBody(response);
        MusicComment musicComment = JSON.parseObject(result, MusicComment.class);
        if(musicComment.getComments().size() > 0) {
            this.commentSaveHandle.save(response, p);
        }
        log.debug(result);
        if (!musicComment.isMore() || musicComment.getTotal() < ((CommentParams)p).getOffset()) this.more = false;
    }
}
